using System;
using System.Collections.Generic;
using System.Net;
using System.IO;
using System.Text;
using System.Web;
using System.Xml;
using MediaPortal.GUI.Library;
using Atom.Core;


namespace MTVMusicPlugin
{
  public class MTVService
  {
    private Dictionary<Uri, AtomFeed> Cache;
    public List<GenreEntity> Genres = new List<GenreEntity>();

    public MTVService()
    {
      Cache = new Dictionary<Uri, AtomFeed>();
      Genres.Add(new GenreEntity("world_reggae", "World/Reggae"));
      Genres.Add(new GenreEntity("pop", "Pop"));
      Genres.Add(new GenreEntity("metal", "Metal"));
      Genres.Add(new GenreEntity("environmental", "Environmental"));
      Genres.Add(new GenreEntity("latin", "Latin"));
      Genres.Add(new GenreEntity("randb", "R&B"));
      Genres.Add(new GenreEntity("rock", "Rock"));
      Genres.Add(new GenreEntity("easy_listening", "Easy Listening"));
      Genres.Add(new GenreEntity("jazz", "Jazz"));
      Genres.Add(new GenreEntity("country", "Country"));
      Genres.Add(new GenreEntity("hip_hop", "Hip-Hop"));
      Genres.Add(new GenreEntity("classical", "Classical"));
      Genres.Add(new GenreEntity("electronic_dance", "Electronic / Dance"));
      Genres.Add(new GenreEntity("blues_folk", "Blues / Folk"));
      Genres.Add(new GenreEntity("alternative", "Alternative"));
      Genres.Add(new GenreEntity("soundtracks_musicals", "Soundtracks / Musicals"));
    }

    public  MTVVideoResponse VideoSearch(string term, int maxres, int startidx)
    {
      MTVVideoResponse resp = new MTVVideoResponse();
      StringBuilder data = new StringBuilder();
      data.Append("term=" + HttpUtility.UrlEncode(term));
      if(maxres>0)
        data.Append("&max-results="+maxres.ToString());
      if (startidx > 0)
        data.Append("&start-index=" + startidx.ToString());
      string url = string.Format("http://api.mtvnservices.com/1/video/search/?{0}", data.ToString());
      resp.Feed = LoadFeed(new Uri(url));
      return resp;
    }

    public  MTVArtistResponse ArtistSearch(string term, int maxres, int startidx)
    {
      MTVArtistResponse resp = new MTVArtistResponse();
      StringBuilder data = new StringBuilder();
      data.Append("term=" + HttpUtility.UrlEncode(term));
      if (maxres > 0)
        data.Append("&max-results=" + maxres.ToString());
      if (startidx > 0)
        data.Append("&start-index=" + startidx.ToString());
      string url = string.Format("http://api.mtvnservices.com/1/artist/search/?{0}", data.ToString());
      resp.Feed = LoadFeed(new Uri(url));
      return resp;
    }

    public  MTVArtistResponse Artist(string url)
    {
      MTVArtistResponse resp = new MTVArtistResponse();
      resp.Feed = LoadFeed(new Uri(url));
      return resp;
    }

    public MTVVideoResponse Video(string url)
    {
      MTVVideoResponse resp = new MTVVideoResponse();
      resp.Feed = LoadFeed(new Uri(url));
      return resp;
    }

    public MTVArtistResponse ArtistBrowse(string leter)
    {
      MTVArtistResponse resp = new MTVArtistResponse();
      string url = string.Format("http://api.mtvnservices.com/1/artist/browse/{0}", leter);
      resp.Feed = LoadFeed(new Uri(url));
      return resp;
    }

    public AtomFeed LoadFeed(Uri url)
    {
      Log.Debug("Loading feed from: {0}", url);
      if (Cache.ContainsKey(url))
      {
        return Cache[url];
      }
      else
      {
        AtomFeed feed = AtomFeed.Load(url);
        Cache.Add(url, feed);
        return feed;
      }
    }
  }
}
